package storage

import (
	"testing"
	"time"

	"github.com/aliyun/aliyun-oss-go-sdk/oss"
	. "github.com/smartystreets/goconvey/convey"
)

func TestCheckTokenValid(t *testing.T) {
	now := time.Now()
	Convey("Test oss token check", t, func() {
		So(checkStsTokenRespValid(nil, 0), ShouldBeFalse)
		So(checkStsTokenRespValid(&StsTokenResp{}, 0), ShouldBeFalse)
		So(checkStsTokenRespValid(&StsTokenResp{
			RequestId: "123",
		}, 0), ShouldBeFalse)
		So(checkStsTokenRespValid(&StsTokenResp{
			RequestId: "123",
			Credentials: Credentials{
				Expiration: "1234",
			},
		}, 0), ShouldBeFalse)

		//0 < 1s
		So(checkStsTokenRespValid(&StsTokenResp{
			RequestId: "123",
			Credentials: Credentials{
				Expiration: time.Now().UTC().Format(time.RFC3339),
			},
		}, 1), ShouldBeFalse)

		//500ms > 0s
		tA50 := now.Add(1 * time.Second)
		So(checkStsTokenRespValid(&StsTokenResp{
			RequestId: "123",
			Credentials: Credentials{
				Expiration: tA50.UTC().Format(time.RFC3339),
			},
		}, 0), ShouldBeTrue)

	})
}

func TestConvertStorateParamterToOssOptions(t *testing.T) {
	Convey("test convert to oss parameters", t, func() {
		So(convertStorageParemterToOssOptions(nil, nil), ShouldBeNil)
		opts := []oss.Option{}
		paramWrong := []StorageParemeter{map[string]string{"a": "b"}}
		So(convertStorageParemterToOssOptions(&paramWrong, &opts), ShouldNotBeNil)

		paramright := []StorageParemeter{map[string]string{"ContentType": "123"}}
		So(convertStorageParemterToOssOptions(&paramright, &opts), ShouldBeNil)
	})
}
